﻿(function($)
{
    $.fn.autoloadSelect = function(options)
    {
        var defaults =
        {
            key: "id",
            value: "label",
            data: {}
        };

        var settings = $.extend({}, defaults, options);
        var target = this;
        if (typeof settings.trigger != 'undefined')
        {
            if (!(settings.trigger instanceof $)) settings.trigger = $(settings.trigger);
            if (typeof settings.now == 'undefined') settings.now = false;

            settings.trigger.each(function()
            {
                settings.trigger.change(function()
                {
                    internal_autoloadSelect.apply(target, [settings.trigger[0].name, settings.trigger.val()]);
                });
            });
        }
        else if (typeof settings.now == 'undefined') settings.now = true;

        if (settings.now)
            internal_autoloadSelect();
        return this;


        function internal_autoloadSelect(name, val)
        {
            var data = settings.data;
            if (typeof name != 'undefined')
            {
                if (typeof settings.data == 'string')
                {
                    data = data + '&' + name + '=' + val;
                }
                else if (typeof settings.data == 'object')
                {
                    data[name] = val;
                }
            }
            target.empty();

            $.ajax({
                url: settings.url,
                data: data,
                type: (settings.type || 'get'),
                dataType: 'json',
                success: function(j)
                {
                    var i = 0, o = null, selectionSet = false;

                    for (i = 0; i < j.length; i++)
                    {
                        // required to get around IE bug (http://support.microsoft.com/?scid=kb%3Ben-us%3B276228)
                        o = document.createElement("OPTION");
                        if (typeof j[i] == 'object')
                        {
                            o.value = j[i][settings.key];
                            o.text = j[i][settings.value];
                            if (typeof j[i].selected == 'boolean')
                            {
                                o.selected = j[i].selected;
                                if (j[i].selected)
                                    selectionSet = true;
                            }
                        }
                        else
                        {
                            o.value = j[i]; o.text = j[i];
                        }
                        target[0].options[i] = o;
                    }

                    if (!selectionSet)
                    {
                        target.find('option:first')
                                 .attr('selected', 'selected');
                    }
                    // hand control back to browser for a moment
                    setTimeout(function()
                    {
                        target//                                 .parent('select')
                                 .trigger('change', true);
                    }, 0);
                },  // end success()
                error: function(xhr, desc, er)
                {
                    // add whatever debug you want here.
                    alert("an error occurred");
                }
            }); // end ajax
        }  // end internal_autoSelect
    }
    $.fn.selectChain = function(options)
    {
        $(options.target).autoloadSelect($.extend({ trigger: '#' + this[0].id }, options));
        return this;
    }
})(jQuery);

